﻿using Sustainalytics.Claims;
using Sustainalytics.ScreeningTool.BL;
using Sustainalytics.ScreeningTool.Service.Utility;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Sustainalytics.ScreeningTool.Service
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/criterialevel1")]
    public class CriteriaLevel1Controller : ApiController, ICriteriaLevel1Controller
    {
        private readonly ICompanyScreening _companyScreening;
        private readonly IClaimsChecker _claimsChecker;
       
        public CriteriaLevel1Controller(
            ICompanyScreening companyScreening, 
            IClaimsChecker claimsChecker)
        {
            _companyScreening = companyScreening;
            _claimsChecker = claimsChecker;
        }
       
        //[CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("")]
        public HttpResponseMessage Get([FromUri]ListOptions options, bool includeAllFields = false, string username = "")
        {
            try
            {
                var criteriaLevelOneList = _companyScreening.ListCriteriaLevelOne().OrderBy(c => c.Order).ToList();
                if (includeAllFields)
                {
                    var criteriaLevelTwoList =
                        _companyScreening.ListCriteriaLevelTwo(null).OrderBy(c => c.Order).ToList();

                    var baseCriteriaList = _companyScreening.ListBaseCriteria(null).Where(a => a.isActiveCategory == null || (a.isActiveCategory ?? a.isActiveCategory.Value == true))
                        .OrderBy(a => a.Order).ToList();

                    List<CriteriaLevel1Dto> criteriaTabs = new List<CriteriaLevel1Dto>();
                    criteriaLevelOneList.ForEach(cr1 =>
                    {
                        var criteriaLevel2List =
                            criteriaLevelTwoList.Where(cr2 => cr2.CriteriaLevel1Id == cr1.Id).ToList();
                        criteriaTabs.Add(CriteriaLevel1Dto.CreateComplete(cr1,
                            criteriaLevel2List, baseCriteriaList, this,
                            _claimsChecker));
                    });

                    return Request.CreateResponse(HttpStatusCode.OK, criteriaTabs.Where(x=> x != null));
                }

                return Request.CreateResponse(HttpStatusCode.OK,
                    criteriaLevelOneList.ConvertAll<object>(
                        cr1 => CriteriaLevel1Dto.CreateWithNameId(cr1, this, _claimsChecker)));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new { Code = ErrorCodes.ScreeningTool_LoadingCriteriaTabs });
            }
        }

        //[CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("{id}")]
        public HttpResponseMessage Get(Guid id, bool includeAllFields = false, string username = "")
        {
            try
            {
                var criteriaLevelOne = _companyScreening.GetCriteriaLevelOne(id);
                if (includeAllFields)
                {
                    var criteriaLevelTwoList = _companyScreening.ListCriteriaLevelTwo(null);
                    var baseCriteriaList = _companyScreening.ListBaseCriteria(null);

                    var criteriaLevel2List = criteriaLevelTwoList.Where(cr2 => cr2.CriteriaLevel1Id == criteriaLevelOne.Id).ToList();
                    return Request.CreateResponse(HttpStatusCode.OK,
                        CriteriaLevel1Dto.CreateComplete(criteriaLevelOne, criteriaLevel2List, baseCriteriaList, this,
                            _claimsChecker));
                }

                return Request.CreateResponse(HttpStatusCode.OK,
                    CriteriaLevel1Dto.CreateWithNameId(criteriaLevelOne, this, _claimsChecker));

            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new { Code = ErrorCodes.ScreeningTool_LoadingCriteriaTabs });
            }
        }
    }
}
